package org.example.code.test2025.one_month;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: test0102
 * @Author yxy
 * @Package org.example.code.test2025.one_month
 * @Date 2025/1/2 13:54
 * @description:
 */
public class test0102 {
    public String countAndSay(int n) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list = fun(1, n, list);
        StringBuilder sb = new StringBuilder();
        for (Integer integer : list) {
            sb.append(integer);
        }
        return sb.toString();
    }

    public List<Integer> fun(int k, int n, List<Integer> list) {
        if (k == n) {
            return list;
        }
        List<Integer> result = new ArrayList<>();
        int forn = 0, num = 0;
        for (Integer integer : list) {
            if (forn == 0) {
                forn = integer;
                num = 1;
            } else if (integer == forn) {
                num++;
            } else {
                result.add(num);
                result.add(forn);
                num = 1;
                forn = integer;
            }
        }
        result.add(num);
        result.add(forn);
        return fun(k + 1, n, result);
    }

    public int divide(int dividend, int divisor) {
        if (dividend == Integer.MIN_VALUE && divisor == -1) {
            return Integer.MAX_VALUE;
        }
        if (dividend == Integer.MIN_VALUE && divisor == 1) {
            return Integer.MIN_VALUE;
        }

        int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1;
        long absDividend = Math.abs((long) dividend);
        long absDivisor = Math.abs((long) divisor);

        long sum = 0;
        while (absDividend >= absDivisor) {
            long temp = absDivisor, multiple = 1;
            while (absDividend >= (temp << 1)) {
                temp <<= 1;
                multiple <<= 1;
            }
            absDividend -= temp;
            sum += multiple;
        }
        sum = sign == 1 ? sum : -sum;
        if (sum > Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        }
        if (sum < Integer.MIN_VALUE) {
            return Integer.MIN_VALUE;
        }
        return (int) sum;
    }

    public static void main(String[] args) {
        test0102 t = new test0102();
        System.out.println(t.divide(-2147483647, 2));
    }
}
